<template>
  <div>
    <div class="from">
      <div class="header">
        <h4>网格区域</h4>
        <el-button type="success" size="small" @click="add">新增 + </el-button>
      </div>
      <hr />
      <div class="table">
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column prop="gridName" label="网格名称" align="center"> </el-table-column>
          <el-table-column prop="number" label="网格编号" align="center"> </el-table-column>
          <el-table-column prop="address" label="操作" align="center">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleclick(scope.row, 1)">编辑</el-button>
              <el-button size="mini" type="danger" @click="handleclick(scope.row, 2)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="page">
        <el-pagination background layout="prev, pager, next" :page-count="total" @current-change="change">
        </el-pagination>
      </div>
    </div>
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <el-form ref="form" :model="sent" label-width="80px">
        <el-form-item label="网格名称:" prop="gridName">
          <el-input v-model="sent.gridName" style="width: 65%"></el-input>
        </el-form-item>
        <el-form-item label="网格编号:" prop="number">
          <el-input v-model="sent.number" style="width: 65%"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="del">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import superadmin from '@/api/superadmin'
export default {
  data() {
    return {
      tableData: [],
      region: 1,
      limit: 6,
      dialogVisible: false,
      tableMessag: {},
      sent: {
        gridName: '',
        id: '',
        number: '',
      },
      id: '',
      total: 0,
      addstatus: 0,
    }
  },
  methods: {
    patrol() {
      superadmin.patrol(this.region, this.limit).then(e => {
        this.tableData = e.data.List.records
        this.total = e.data.pages
      })
    },
    handleclick(scope, val) {
      switch (val) {
        case 1:
          this.addstatus = 0
          this.tableMessag = scope
          this.sent.gridName = scope.gridName
          this.sent.number = scope.number
          this.sent.id = scope.id
          this.dialogVisible = true
          break
        case 2:
          this.id = scope.id
          superadmin.RRdelete(this.id).then(e => {
            if (e.code == 200) {
              this.$message({
                message: '删除成功!',
                type: 'success',
              })
              this.patrol()
            }
          })
          break
        default:
          break
      }
    },
    submit() {
      if (this.sent.gridName == '' || this.sent.number == undefined) {
        this.$message({
          message: '请输入完整内容后再提交！',
          type: 'error',
        })
      } else {
        superadmin.createOrUpdate(this.sent).then(e => {
          if (e.code == 200) {
            this.$message({
              message: '操作成功！',
              type: 'success',
            })
            this.sent.gridName = ''
            this.sent.number = ''
            this.sent.id = ''
            this.dialogVisible = false
            this.patrol()
          } else if (e.code == 5007) {
            this.$message({
              message: e.data,
              type: 'error',
            })
          }
        })
      }
    },
    change(val) {
      this.region = val
      this.patrol()
    },
    del() {
      this.dialogVisible = false
      this.sent.gridName = ''
      this.sent.number = ''
      this.sent.id = ''
    },
    add() {
      this.dialogVisible = true
      this.addstatus = 1
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.sent.gridName = ''
          this.sent.number = ''
          this.sent.id = ''
          done()
        })
        .catch(_ => {})
    },
  },
  created() {
    this.patrol()
  },
}
</script>

<style scoped lang="scss">
@import './region.scss';
</style>
